<template>
  <page-layout class="demand">
    <div class="search-wrapper">
      <div>
        <a-input placeholder="请输入需求名称" class="search-input">
          <a-icon
            slot="prefix"
            type="search"
            style="font-size: 28px; color: #999"
          />
        </a-input>
        <a-button type="primary" class="search-button">搜索</a-button>
      </div>
      <!-- <a-button style="width: 120px; height: 54px" type="primary"
        >新增</a-button
      > -->
    </div>

    <!-- <a-card> -->
    <div class="tabel-wrapper">
      <!-- <div class="operator">
          <a-button @click="addNew" type="primary">新建</a-button>
          <a-button>批量操作</a-button>
          <a-dropdown>
            <a-menu @click="handleMenuClick" slot="overlay">
              <a-menu-item key="delete">删除</a-menu-item>
              <a-menu-item key="audit">审批</a-menu-item>
            </a-menu>
            <a-button> 更多操作 <a-icon type="down" /> </a-button>
          </a-dropdown>
        </div> -->
      <standard-table
        :columns="columns"
        :dataSource="dataSource"
        @change="onChange"
        :pagination="pagination"
        size="large"
        :loading="loading"
      >
        <div slot="changes" slot-scope="{ record }">
          <template>
            <a
              style="text-decoration: underline"
              @click="change_infos(record.key)"
              title="点击查看需求变更记录"
            >
              查看变更记录
            </a>
          </template>
        </div>
        <!-- <div slot="status" slot-scope="{ text }">
          <a-tag
            :style="`
              width: 70px;
              height: 30px;
              text-align: center;
              line-height: 30px;
            `"
            :color="`${text === 1 ? '#00CA13' : '#FF2525'}`"
          >
            {{ text === 1 ? "已审核" : "已终止" }}
          </a-tag>
        </div> -->
        <!-- <div slot="process" slot-scope="{ text, record }">
          <router-link to="/demand/detail" :key-id="record.key">
            查看流程
          </router-link>
        </div> -->
        <!-- eslint-disable-next-line -->
        <div slot="action" slot-scope="{ text, record }">
          <a
            :href="`#/demand/detail?random=${random}&&id=${record.key}&&finish=1`"
            style="margin-right: 8px"
            >详情
          </a>
        </div>
        <template slot="statusTitle">
          <a-icon @click.native="onStatusTitleClick" type="info-circle" />
        </template>
      </standard-table>
    </div>
    <a-modal
      v-model="changes_visible"
      title="变更记录"
      ok-text="确认"
      @ok="hideModal"
      @cancel="hideModal"
      width="580px"
      :mask="false"
    >
      <a-table bordered :data-source="change_datas" :columns="change_columns">
        <!-- <template slot="name" slot-scope="text, record">
                  <editable-cell :text="text" @change="onCellChange(record.key, 'name', $event)" />
                </template> -->
        <div slot="content" slot-scope="content" v-html="content"></div>
        <template slot="change_status" slot-scope="change_status">
          <p>
            <a href="javascript:;" style="color: " v-if="change_status == 2"
              >已审核</a
            >
            <a href="javascript:;" style="color: gray" v-else>已中止</a>
          </p>
        </template>

        <template slot="files" slot-scope="files">
          <a v-if="!files || files.length === 0">暂无附件</a>
          <!-- <div  class="enclosure-wrapper"> -->
          <div v-else class="enclosure-wrapper">
            <div
              class="enclosure-item"
              v-for="file in JSON.parse(files)"
              :key="file.uid"
            >
              <a-icon type="file-zip" theme="twoTone" style="font-size: 50px" />
              <a
                :href="
                  file.response && file.response.data && file.response.data.url
                "
                download:void
                class="enclosure-content"
                >{{ file.name }}</a
              >
            </div>
          </div>
        </template>
      </a-table>
    </a-modal>
    <!-- </a-card> -->
  </page-layout>
</template>

<script>
import PageLayout from "@/layouts/PageLayout";
import StandardTable from "@/components/table/StandardTable";
import { list, deleteItem, changes } from "@/services/demand";
// import { mapMutations } from "vuex";
const change_columns = [
  {
    title: "id",
    dataIndex: "id",
    scopedSlots: { customRender: "change_id" },
    key: "id",
    width: 60
    //   scopedSlots: { customRender: 'name' },
  },
  {
    title: "备注内容",
    dataIndex: "content",
    scopedSlots: { customRender: "content" },
    key: "content"
  },
  {
    title: "附件",
    dataIndex: "files",
    key: "files",
    scopedSlots: { customRender: "files" }
  },
  {
    title: "提交时间",
    dataIndex: "created_at",
    key: "created_at",
    ellipsis: true
  },
  {
    title: "审核状态",
    dataIndex: "change_status",
    scopedSlots: { customRender: "change_status" },
    align: "center"
  }
];
const columns = [
  {
    title: "ID",
    dataIndex: "key"
    // sorter: true
  },
  {
    title: "所属部门",
    dataIndex: "department"
    // sorter: true
  },
  {
    title: "需求名称",
    dataIndex: "title"
  },
  {
    title: "发起人",
    dataIndex: "author"
  },
  {
    title: "关联项目",
    dataIndex: "project"
  },
  {
    title: "关联产品",
    dataIndex: "produce"
  },
  {
    title: "需求变更",
    scopedSlots: { customRender: "changes" }
  },
  // {
  //   title: "流程跟踪",
  //   scopedSlots: { customRender: "process" }
  // },
  {
    title: "操作",
    scopedSlots: { customRender: "action" },
    align: "center"
  }
];

export default {
  name: "demand",
  components: { PageLayout, StandardTable },
  data() {
    // console.log(dataSource, 111);
    return {
      changes_visible: false,
      random: "",
      change_columns,
      change_datas: "",
      loading: true,
      searchText: "",
      desc: "需求管理中心",
      advanced: true,
      columns: columns,
      dataSource: [],
      selectedRows: [],
      pagination: {
        size: "small",
        showSizeChanger: true,
        showQuickJumper: true,
        poposition: "bottom",
        pageSize: 5,
        current: 1,
        total: 0
      }
    };
  },
  authorize: {
    deleteRecord: "delete"
  },
  activated() {
    const { pageSize, current } = this.pagination;
    this.random = Math.random();
    this.loadTableData({ limit: pageSize, page: current, status: 4 });
  },
  created() {
    // 初始数据
    const { pageSize, current } = this.pagination;
    this.random = Math.random();
    this.loadTableData({ limit: pageSize, page: current, status: 4 });
  },
  methods: {
    change_infos(id) {
      this.changes_visible = true;
      this.getChanges(id);
    },
    //获取变更数据
    getChanges(id) {
      changes(id).then((res) => {
        console.log(res.data.data);
        if (res.data.code === 20000) {
          this.change_datas = res.data.data;
          // this.$message.success(res.data.message);
        }
      });
    },
    hideModal() {
      this.changes_visible = false;
      this.change_datas = null;
    },

    // 请求table 的数据
    // obj 请求的字段
    loadTableData(obj) {
      this.loading = true;
      list(obj).then((res) => {
        // console.log(res.data, 222222);
        // 关闭loading
        this.loading = false;
        if (res.data.code === 20000) {
          const { current_page, total, per_page } = res.data.data;
          Object.assign(this.pagination, {
            total: parseInt(total),
            current: parseInt(current_page),
            pageSize: parseInt(per_page)
          });
          this.dataSource = res.data.data.data.map((item) => {
            const {
              requirement_id,
              department,
              created_at,
              title,
              author,
              project,
              produce,
              status
            } = item;
            return {
              key: requirement_id,
              department,
              created_at: created_at && created_at.split(" ")[0],
              title: title || "未知",
              author: author || "未知",
              project: project || "未关联项目",
              produce: produce || "未关联产品",
              status
            };
          });
        }
      });
    },
    handelsearch() {
      this.loadTableData({
        limit: this.pagination.pageSize,
        title: this.searchText
      });
      this.searchText = "";
    },

    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    onClear() {
      this.$message.info("您清空了勾选的所有行");
    },
    onStatusTitleClick() {
      this.$message.info("你点击了状态栏表头");
    },
    onChange(e) {
      // console.log(e);
      const { current, pageSize } = e;
      this.loadTableData({ limit: pageSize, page: current, status: 4 });
    },
    handleMenuClick(e) {
      if (e.key === "delete") {
        this.remove();
      }
    },
    cancel(key) {
      this.$message.info(`点击了取消,行数为${key + 1}行`);
    },
    // 删除确认
    confirm(key) {
      // 发起删除的请求
      deleteItem(key).then((res) => {
        console.log(res, "delete");
        // 成功
        if (res.data.code === 20000) {
          // 提示信息
          this.$message.success({
            content: res.data.message || "删除成功~"
          });
          // 删除成功后重新获取数据
          const { pageSize, current } = this.pagination;
          this.loadTableData({ limit: pageSize, page: current, status: 4 });
        } else {
          this.$message.error(res.data.message || "删除失败！");
        }
      });
      // this.$message.info(`点击了确定,行数为${key + 1}行`);
      // this.deleteRecord(key);
    }
  }
};
</script>

<style lang="less">
.search-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;

  > div {
    .search-input input {
      padding-left: 45px !important;
    }
    display: flex;
    width: 60%;
    .ant-input-affix-wrapper {
      display: inline-block;
      width: calc(100% - 150px);
      height: 54px !important;
      input {
        height: 100% !important;
      }
      input::-webkit-input-placeholder {
        color: #9999;
        font-size: 16px;
      }

      input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #9999;
        font-size: 16px;
      }

      input:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: #9999;
        font-size: 16px;
      }

      input:-ms-input-placeholder {
        /* Internet Explorer 10-11 */
        color: #9999;
        font-size: 16px;
      }
    }
    .search-button {
      margin-left: 30px;
      width: 120px;
      height: 54px;
    }
  }
}
.demand {
  .tabel-wrapper {
    border-radius: 5px;
    // min-height: 450px;
    background-color: #fff;
    color: #1f1f1f;
    // .ant-empty {
    //   // min-height: 450px;
    // }
    .ant-pagination {
      margin-top: 60px !important;
    }
    tr {
      height: 66px !important;
      // padding-left: 10px;
    }
    .ant-table-thead {
      padding-left: 20px;
      background: #effaff;
      & > tr > th {
        background: #effaff !important;
      }
    }
    .ant-table-tbody {
      padding-left: 20px;
    }
    .ant-table-pagination.ant-pagination {
      float: unset !important;
      text-align: center;
    }
  }
}
</style>
